﻿using NLog;
using NLog.Web;
using SXFramWork.Core;
using System;
using System.Text;

namespace SXFramWork.Core
{
    /// <summary>
    /// ****Nlog日志帮助类****
    /// Trace 包含大量的信息，例如 protocol payloads。一般仅在开发环境中启用, 仅输出不存文件。
    /// Debug  比 Trance 级别稍微粗略，一般仅在开发环境中启用, 仅输出不存文件。
    /// Info 一般在生产环境中启用。
    /// Warn 一般用于可恢复或临时性错误的非关键问题。
    /// Error 一般是异常信息。
    /// Fatal - 非常严重的错误！
    /// </summary>
    public class Loghelper
    {
        readonly Logger logger;

        private Loghelper(Logger logger)
        {
            this.logger = logger;
        }
        public static Loghelper Default { get; private set; }
        static Loghelper()
        {
            Default = new Loghelper(NLogBuilder.ConfigureNLog("NLog.config").GetLogger(DateTime.Now.ToString("yyyy-MM-dd")));
        }
        public void Debug(string msg, params object[] args)
        {
            logger.Debug(msg, args);
        }
        public void Debug(string msg, Exception err)
        {
            logger.Debug(err, msg);
        }

        public void Info(string msg, params object[] args)
        {
            logger.Info(msg, args);
        }

        public void Info(string msg, Exception err)
        {
            logger.Info(err, msg);
        }

        public void Trace(string msg, params object[] args)
        {
            logger.Trace(msg, args);
        }

        public void Trace(string msg, Exception err)
        {
            logger.Trace(err, msg);
        }

        public void Error(string msg, params object[] args)
        {
            logger.Error(msg, args);
        }

        public void Error(string msg, Exception err)
        {
            logger.Error(err, msg);
        }

        public void Fatal(string msg, params object[] args)
        {
            logger.Fatal(msg, args);
        }

        public void Fatal(string msg, Exception err)
        {
            logger.Fatal(err, msg);
        }
        /// <summary>
        /// 写操作日志到数据库
        /// </summary>
        /// <param name="username"></param>
        /// <param name="caozuo"></param>
        /// <param name="msg"></param>
        /// <param name="type"></param>
        /// <param name="result"></param>
        /// <param name="elapsedMilliseconds"></param>
        public void writedblog(string username, string caozuo, string msg="",int type=0,int result=0,long elapsedMilliseconds=0)
        {
            LogEventInfo lei = new LogEventInfo();
            lei.Properties["Operation"] = caozuo;
            lei.Properties["Id"] = GuidHelper.NewSequentialGuid(SequentialGuidType.SequentialAtEnd).To_String();
            lei.Properties["UserName"] = username;
            lei.Properties["Type"] = type;
            lei.Properties["Result"] = result;
            lei.Properties["ElapsedMilliseconds"] = elapsedMilliseconds;
            lei.Level = LogLevel.Info;
            lei.Message = msg;
            logger.Log(lei);
        }
        /// <summary>
        /// 功能:写错误日志到文件
        /// </summary>
        /// <param name="exception">异常</param>
        /// <param name="err">错误描述</param>
        public string writelog(Exception exception, string err = "")
        {
            if (exception == null) return "";
            StringBuilder sb = new StringBuilder();
            writeerrorlog(sb, exception);
            var errstr = sb.ToString() + err;
            LogEventInfo lei = new LogEventInfo();
            lei.Level = LogLevel.Error;
            lei.Message = errstr;
            logger.Log(lei);
            return errstr;
        }
        /// <summary>
        /// 功能:写底层错误信息
        /// </summary>
        /// <param name="err">错误描述</param>
        public string writelog(string err = "")
        {
            StringBuilder sb = new StringBuilder();
            var errstr = sb.ToString() + err;
            LogEventInfo lei = new LogEventInfo();
            lei.Level = LogLevel.Error;
            lei.Message = errstr;
            logger.Log(lei);
            return errstr;
        }
        /// <summary>
        /// 功能:构造错误日志
        /// </summary>
        /// <param name="sb">存储错误日志</param>
        /// <param name="exception">异常</param>
        private static void writeerrorlog(StringBuilder sb, Exception exception)
        {
            sb.Append("错误类型:" + exception.GetType().Name + "\r\n");
            sb.Append("错误信息:" + exception.Message + "\r\n");

            if (!string.IsNullOrEmpty(exception.StackTrace))
                sb.Append("StackTrace:\r\n" + exception.StackTrace + "\r\n");

            if (exception.InnerException != null)
            {
                sb.Append("InnerException:\r\n");
                writeerrorlog(sb, exception.InnerException);
            }
        }
    }
}